// pages/gooddetail/gooddetail.js
var common = require("../../public");
var url = common.url;
var APP_ID = common.APP_ID;
var md5 = common.md5Start;
var WxParse = require('../../wxParse/wxParse.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
		author_alert:false,//是否显示授权弹框
        indicatorDots: false,
        autoplay: false,
        interval: 5000,
        duration: 1000,
        current: 1,//当前的轮播图是第几张
		scrollid:'good',//要跳转到的id
    },
	//点击进入战场页面
	join_warfield:function(e){
		var id = e.currentTarget.dataset.warid;//战场id
		var session_str = wx.getStorageSync('session_str');//获取密钥
		if (!session_str) {
			wx.navigateTo({
				url: '../author/author'
			})
			return false;
		}
		var comefrom = this.data.comefrom;//从哪里进入的商品详情  1：从战场进入  else正常进入
		if (comefrom==1){
			wx.navigateBack({
				delta: 1,
			})
		}else{
			wx.navigateTo({
				url: '../gouwuzhanchang/gouwuzhanchang?id=' + id,
			})
		}
		
	},
	//立即购买按钮
	tap_buy_now:function(e){
		var that = this;
		var session_str = wx.getStorageSync('session_str');//获取密钥
		if (!session_str){
			that.setData({
				author_alert:true
			})
			return false;
		}
		var id = this.data.id;//商品id
		wx.navigateTo({
			url: '../order_confirm/order_confirm?id='+id
		})
	},
	// 关闭授权弹框
	closeMask:function(e){
		this.setData({
			author_alert:false,//隐藏授权弹框
		})
	},
	//滚动页面--让导航跟着变化
	scrollpage:function(e){
		// var that = this;
		// var ratio = wx.getSystemInfoSync().windowWidth/750;//系数
		// console.log(ratio)
		// console.log(e.detail.scrollTop)
		// var cal_scroll = ratio*e.detail.scrollTop;
		// console.log(cal_scroll)
		// if (cal_scroll<=326){
		// 	that.setData({
		// 		scrolltag:'good',
		// 		scrollid:''
		// 	})
		// }
		// if (cal_scroll > 326 && cal_scroll<=442){
		// 	that.setData({
		// 		scrolltag: 'pingjia',
		// 		scrollid: ''
		// 	})
		// } 
		// if (cal_scroll > 442){
		// 	that.setData({
		// 		scrolltag: 'detail',
		// 		scrollid: ''
		// 	})
		// }
	},
	//跳转授权页面
	goAuthor:function(e){
		wx.navigateTo({
			url: '../author/author'
		})
	},
	//收藏、取消收藏
	collection_fun:function(e){
		var that = this;
		var session_str = wx.getStorageSync('session_str');//密钥
		if (!session_str){
			that.setData({
				author_alert:true
			})
			return false;
		}
		var goods_id = this.data.id;//商品id
		var is_collection = this.data.is_collection;//1：已收藏   0：为收藏
		if (is_collection==1){
			//如果已经收藏    请求取消收藏接口
			wx.request({
				url: url + 'User/cancelCollection',
				data: {
					session_str: session_str,
					goods_id: goods_id,
					APP_ID: APP_ID,
					sign: md5([
						"session_str" + session_str,
						"goods_id" + goods_id,
						"APP_ID" + APP_ID,
					])
				},
				header: {},
				method: 'post',
				dataType: 'json',
				responseType: 'text',
				success: function (res) {
					wx.hideLoading();
					if (res.data.code == 200) {
						that.setData({
							is_collection: 0
						})
						wx.showToast({
							title: '取消收藏成功',
							icon: 'success',
							mask: true,
						})
					} else {
						wx.showToast({
							title: res.data.msg,
							icon: 'none',
							mask: true,
						})
					}

				},
				fail: function (res) {
					wx.hideLoading();
					wx.showToast({
						title: '网络错误',
						icon: 'none',
						mask: true,
					})
				}
			});
		}else{
			//如果未收藏    请求收藏接口
			wx.request({
				url: url + 'User/addCollection',
				data: {
					session_str: session_str,
					goods_id: goods_id,
					APP_ID: APP_ID,
					sign: md5([
						"session_str" + session_str,
						"goods_id" + goods_id,
						"APP_ID" + APP_ID,
					])
				},
				header: {},
				method: 'post',
				dataType: 'json',
				responseType: 'text',
				success: function (res) {
					wx.hideLoading();
					if (res.data.code == 200) {
						that.setData({
							is_collection: 1
						})
						wx.showToast({
							title: '收藏成功',
							icon: 'success',
							mask: true,
						})
					} else {
						wx.showToast({
							title: res.data.msg,
							icon: 'none',
							mask: true,
						})
					}

				},
				fail: function (res) {
					wx.hideLoading();
					wx.showToast({
						title: '网络错误',
						icon: 'none',
						mask: true,
					})
				}
			})
		}
	},
	//跳转评价详情
	go_pingjia_detail:function(e){
		
		var id = this.data.id;//获取商品id 
		wx.navigateTo({
			url: '../pingjia_all/pingjia_all?id='+id
		})
	},
	//切换导航
	changenav:function(e){
		var scrollid = e.currentTarget.dataset.id;
		console.log(scrollid);
		this.setData({
			scrollid: scrollid
		})
	},
    // 轮播图tab的切换
	bindswiperchange: function(e) {
        console.log(e.detail.current)
		this.setData({
			current: e.detail.current-0+1
		})
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
		this.setData({
			id:options.id,//商品id
			comefrom: options.comefrom,//1：从战场进入的  如果没有则是从商品列表进入
		})
		var that = this;
		
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
		var that = this;
		var id = this.data.id;//商品id
		var session_str = wx.getStorageSync('session_str');//获取密钥
		var res_obj={};//接口请求要传的数据
		if (!session_str){
			res_obj={
				goods_id: id,
				APP_ID: APP_ID,
				sign: md5([
					"goods_id" + id,
					"APP_ID" + APP_ID
				])
			}
		}else{
			res_obj = {
				goods_id: id,
				session_str: session_str,
				APP_ID: APP_ID,
				sign: md5([
					"goods_id" + id,
					"session_str" + session_str,
					"APP_ID" + APP_ID
				])
			}
		}
		//请求商品详情的接口
		wx.request({
			url: url + 'Goods/goodsInfo',
			data: res_obj,
			header: {},
			method: 'post',
			dataType: 'json',
			responseType: 'text',
			success: function (res) {
				wx.hideLoading();
				if(res.data.code==200){
					that.setData({
						goodinfo:res.data.data,
						is_collection: res.data.data.is_collection,//1:已收藏   0：为收藏
						id: res.data.data.goods_id,//商品id
					})
					var article = res.data.data.goods_info;//商品介绍  富文本

					WxParse.wxParse('article', 'html', article, that, 5);
				}else{
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						mask: true,
					})
				}
				
			},
			fail: function (res) {
				wx.hideLoading();
				wx.showToast({
					title: '网络错误',
					icon: 'none',
					mask: true,
				})
			}
		});
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})